
    <div class="ibox float-e-margins">
        <div class="ibox-title" style="border: none;">
            <h5 style="color: #00afef"><i>New Payment</i></h5>
        </div>
        <div class="ibox-content">
            <form class="form-horizontal" id="search_payment_form">
                <div class="form-group">
                <label class="col-lg-4 control-label">Account #</label>
                    <div class="col-lg-5">
                        <select style="" id="" class="chosen-select" name="acct_num">
                            <?php foreach ($accounts as $acct) { ?>
                            <option value="<?php echo $acct->account_id; ?>" ><?php echo $acct->acct_num.' - '.$acct->acct_name; ?></option>
                            <?php } ?>
                        </select>
                    </div>
                </div>
                <div style="display: none;" class="form-group">
                    <label class="col-lg-4 control-label">Account Name</label>
                    <div class="col-lg-5">
                        <input type="text" name="acct_name" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label"></label>
                    <div class="col-lg-5">
                        <button type="button" class="btn btn-primary btn-md pull-right m-t-n-xs search-btn" id="search_payment"> Go</button>
                        <i id="loading-icon-search2" style="color: gray; display: none ;margin-left: 150px;" class="fa fa-spinner fa-spin fa-lg"></i>
                    </div>
                </div>
            </form>
        </div><!-- ibox content END -->
        <form class="form-horizontal" id="payment_process_form">
        <div id="process_div" style='display:none;'class="ibox-content">
            <div class="form-group">
                <label class="col-lg-4 control-label">Total Unpaid Amount</label>
                <div class="col-lg-5">
                    <input type="number" style="text-align:right;" disabled id="unpaid_amount1" class="form-control">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-4 control-label">Payment Type</label>
                <div class="col-lg-5">
                    <select name="payment_type" class="form-control m-b">
                        <option value="CASH">CASH</option>
                        <option value="CHEQUE">CHEQUE</option>
                    </select>
                </div>
            </div>
             <div id="checkno" class="form-group" style="display: none;">
                <label class="col-lg-4 control-label">Cheque No.</label>
                <div class="col-lg-5">
                    <input type="text" style="text-align:right;" min="0" name="cheque_no" class="form-control">
                </div>
            </div>
             <div class="form-group">
                <label class="col-lg-4 control-label">Payment Amount</label>
                <div class="col-lg-5">
                    <input type="number" style="text-align:right;" value="0.00" min="0" name="payment_account" class="form-control">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-4 control-label">Receipt Number</label>
                <div class="col-lg-5">
                <?php foreach ($receipt as $key): ?>
                    <input readonly value="<?php echo $key->val; ?>" style="text-align:right;" type="text" name="receipt_number" class="form-control">
                <?php endforeach;?>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-4 control-label">Receipt Date</label>
                <div class="col-lg-5">
                    <input type="date" name="receipt_date" class="form-control">
                </div>
            </div>
             <div class="form-group">
                <label class="col-lg-4 control-label">Reference</label>
                <div class="col-lg-5">
                    <input type="text" name="reference" class="form-control">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-4 control-label">Remarks</label>
                <div class="col-lg-5">
                    <input type="text" name="remarks" class="form-control">
                </div>
            </div>
            <h4 class="page-header"><small>Unpaid Invoices of&nbsp;&nbsp;</small><span id="nameofacct"></span></h4>
           <div class="table-responsive">
                <table class="table table-striped table-hover" id="table_invoice">
                <thead>
                    <tr>
                        <th>Invoice #</th>
                        <th>Invoice Type</th>
                        <th>Invoice Date</th>
                        <th>Due Date</th>
                        <th>Total Amt</th>
                        <th>Paid Amt</th>
                        <th style="width: 90px;">Unpaid Amt</th>
                        <th>Process</th>
                    </tr>
                </thead>
                <tbody id='tbl_payments'>

                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="6" align="right">Total Unpaid Amount</td>
                        <td colspan="2" align="right"><input  id="unpaid_amount" readonly step="0.01" type="text" class="form-control" style="text-align: right;"></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td colspan="6" align="right">Total Selected Amount</td>
                        <td colspan="2" align="right" id=""><input  id="selected_amount" readonly step="0.01" type="text" class="form-control" style="text-align: right;"></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td colspan="6" align="right">Total UnSelected Amount</td>
                        <td colspan="2" align="right" id=""><input  id="unselected_amount" readonly step="0.01" type="text" class="form-control" style="text-align: right;"></td>
                        <td></td>
                    </tr>
                </tfoot>
            </table>
           </div>
            <br><br>
                <div class="form-group">
               <!--  <label class="col-lg-4 control-label">Selected Amount</label>
                   <div class="col-lg-5">
                       <input readonly style="text-align:right;" type="number" name="selected_amount" class="form-control" value="0.00">
                   </div>
               </div>
               <div class="form-group">
                   <label class="col-lg-4 control-label">Balance</label>
                   <div class="col-lg-5">
                       <input type="hidden" name="balance2" value="" class="form-control">
                       <input readonly type="number" style="text-align:right;" name="balance" value="0.00" class="form-control">
                   </div>
               </div> -->
                <div class="form-group">
                    <label class="col-lg-5 control-label"></label>
                    <div class="col-lg-3">
                    <!-- data-toggle="modal" data-target="#receipt" -->
                        <button type="button"  class="btn btn-primary btn-block btn-md m-t-n-xs" id="process_payment">Process&nbsp;<i id="loading-icon-process" style="color: white; display: none ;" class="fa fa-spinner fa-spin fa-lg fa-fw"></i></button>
                    </div>
                </div>
            </form>
        </div> <!-- ibox content END -->
    </div>

    <script type="text/javascript">
    $(document).ready(function() {

        $('#receipt_tbl').html('');
        $('select').chosen({width: '100%'});
        var invoice_count = 0;
        $('#payment_process_form select[name=payment_type]').change(function(event) {
            /* Act on the event */
            if($(this).val() == 'CASH'){
                $('#checkno').hide();
            }else{
                $('#checkno').show();
            }
        });
        function alert_cancel(){
                        if($('#payment_process_form').serialize()!=$('#payment_process_form').data('serialize')){
                        // Form has changed!!!
                        swal({
                          title: "Are you sure?",
                          text: "Your work will not be save",
                          type: "warning",
                          showCancelButton: true,
                          confirmButtonColor: "#DD6B55",
                          confirmButtonText: "Leave",
                          cancelButtonText: "Stay",
                          closeOnConfirm: true,
                          closeOnCancel: true },
                      function (isConfirm) {
                          if (isConfirm) {
                            swal("success");
                              close_add(); active_search();
                              notifyClass("clear","");
                          } else {
                              swal("Cancelled");
                          }
                      });
                        }else{
                            close_add(); active_search();
                            notifymsg("hide","hide");
                        }
                }

                function invalid(){
                        // Form has changed!!!
                        swal({
                          title: "Error",
                          text: "Unable to perform action due to insuffecient payment amount.",
                          type: "error",
                          showCancelButton: false,
                          confirmButtonColor: "#DD6B55",
                          confirmButtonText: "Close",
                          cancelButtonText: "Stay",
                          closeOnConfirm: true,
                          closeOnCancel: true },
                      function (isConfirm) {
                          if (isConfirm) {
                            swal("success");
                          } else {
                              swal("Cancelled");
                          }
                      });
                }

                $('#cancel_add').click(function(){
                    alert_cancel();
                });

                $('#addX').click(function(){
                    alert_cancel();
                });
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green hover',
            radioClass: 'iradio_square-green'
        });
        $('#payment_details_body').slimScroll({
            height: '200px',
            railVisible: true
        });

        $('#payment_process_form input[name=receipt_date]').val(new Date().toJSON().slice(0,10));

        var tmp = 0;
        var sum = 0;
        var tmp_amount = [];
        var invoices = [];
        var total_inv = [];
        $('table').on('change','input[type=checkbox]',function(){
            //var amount = parseFloat($(this).closest('tr').children('.amount').text());
            var amount =  parseFloat(getbal($(this).closest('tr').children('.invoice_id').text()));
            //console.log(amount);
            var invid = $(this).closest('tr').children('.invoice_id').text();
            var total_invoice = $(this).closest('tr').children('.total_invoice').text();
            var s_amount = $('#payment_process_form #selected_amount');
            var us_amount = $('#payment_process_form #unselected_amount');
            var up_amount = parseFloat($('#unpaid_amount1').val()).toFixed(2);
            var cb = $(this).is(':checked');
            var id = parseInt($(this).attr('id'));
            invoices[id] = $(this).attr('name');
            tmp_amount[id] = amount;
            total_inv[id] = total_invoice;
            console.log(id);


            if(sum > $("input[name=payment_account]").val()){
                $(this).prop('checked',false)
                if(id.length != 1 && cb == true ){
                    //alert('Unable to perform action due to insuffecient payment amount.');
                    invalid();
                }
                cb = false;
            }

            if(cb == true){
                sum = sum + amount;
                s_amount.val(sum.toFixed(2));
                parseFloat(us_amount.val(up_amount - sum.toFixed(2))).toFixed(2);
                id = id + 1;
                $('#'+id).removeAttr('disabled');
                //console.log(id);
                tmp = sum;
            }else{
                id = id + 1;
                for(var i = id; i <= $('table input[type=checkbox]').length; i++){
                     $('#'+i).attr('disabled',true);
                     $('#'+i).prop('checked',false);
                }
                for(var i = id-1; i <= $('table input[type=checkbox]').length; i++){
                      tmp_amount.splice(id-1,1);
                      invoices.splice(id-1,1);
                      total_inv.splice(id-1,1);
                }
                sum = eval(tmp_amount.join('+'));
                console.log()
                if(sum === undefined || sum === NaN || sum === ''){
                    sum = 0.00;
                }
                tmp = sum;
                s_amount.val(sum.toFixed(2));
                parseFloat(us_amount.val(up_amount- sum.toFixed(2))).toFixed(2);
            }
                if(tmp === undefined || tmp === NaN || tmp === ''){
                    tmp = 0.00;
                }else{
                    tmp = tmp - parseFloat($('input[name=payment_account]').val());
                    if(tmp < 0){
                       tmp = 0;
                }
            }
            //console.log(total_inv);
            //console.log(tmp_amount);
            $('input[name="balance"]').val(tmp.toFixed(2));
            //console.log(tmp);

        });
        function curbal(data){

        }
        
        function getbal(id){
            var result = '';
            $.ajax({
                url:"<?php echo base_url($this->session->userdata('forajax') . '/getbalance'); ?>",
                method:'post',
                async: false,
                data:{ 'invoice_id' : id },
                dataType:'json',
                success: function (data) {
                    var sum_pay = 0;
                    var paid_amount = 0;
                    console.log(data[0].sum_pay);
                    if(data[0].sum_pay == null){
                        sum_pay = parseFloat(0).toFixed(2);
                    }else{
                        sum_pay = parseFloat(data[0].sum_pay).toFixed(2);
                    }
                    paid_amount = parseFloat(data[0].invoice_total).toFixed(2) - sum_pay;
                    result =  paid_amount;
                },
                error: function (xhr){

                }
            });
            return result;
        }

        $(function(){
            $('input[name=payment_account]').keyup(function(){
                calculateTotal($(this).val());
            });
            $('input[name=payment_account]').blur(function(){
                if($('input[name=payment_account]').val() == ''){
                    $('input[name=payment_account]').val(0);
                }
                calculateTotal($(this).val());
            });
        });

        $(':input[type=number]').on('mousewheel', function(e){
            e.preventDefault();
        });

        function calculateTotal(src) {
            var sum = 0;
            var bal = $('input[name="selected_amount"]').val();

            if(!isNaN(src) && src.length!=0) {
                sum += parseFloat(src);
            }
            $('input[name=totalcost]').val(sum.toFixed(2));
            bal = bal - sum;
            if(bal < 0 || sum < 0){
                bal = 0;
            }
            $('input[name="balance"]').val(bal.toFixed(2));
        }

        var validator = $( "#payment_process_form" ).validate();
        var resibo_id = 0;
        var receipt_app_id = 0;
        $('#process_payment').click(function(){
            var x;
            if(parseFloat($('#payment_process_form input[name=payment_account]').val()) == 0){
                validator.showErrors({
                  "payment_account": "Please Enter an amount."
                });
                notifymsg('danger','<b>Error:</b> Can\'t Process Transaction.');
            }else if(parseFloat($('#payment_process_form input[name=selected_amount]').val()) < parseFloat($('#payment_process_form input[name=payment_account]').val())){
                validator.showErrors({
                  "payment_account": "Oops! The amount you enter is to high from the amount you selected"
                });
                notifymsg('danger','<b>Error:</b> Can\'t Process Transaction.');
            }else{
                if($('#payment_process_form').valid() == true){
                    if (confirm("Process Payments?") == true) {
                    $.ajax({
                        url:"<?php echo base_url($this->session->userdata('forajax') . '/create_resibo'); ?>",
                        method:'post',
                        data:$('#payment_process_form').serialize()+'&'+$.param({ 'account_id':  sessionStorage.account_id, 'selected_amt' : $('#payment_process_form #selected_amount').val() }),
                        dataType:'json',
                        async: false,
                        success: function (data) {
                            resibo_id = data.id;
                        }
                    });
                    //console.log('kigol' + resibo_id);
                    //console.log(invoices.length+ 'invoicessss');
                    //console.log(tmp_amount);
                    var balance = 0; var lastins = 0; var for_lastins = 0; var last_pa = 0;  var last_bal = 0;
                    if(invoices.length == 0){
                        console.log('hello there');
                        $('#search_payment').trigger('click');
                        getReceipt3($('#payment_process_form input[name=receipt_number').val());
                        getrecnum();
                        notifymsg('success','<b>Success</b>: Data Saved.');
                        $('#count_add').val('0');
                        $('#tab-list-2 #close-add-body').parents('li').remove('li');
                        $('#add-link').remove();
                        $('#tab-list-2 a:first').tab('show');
                        gen_advance_row($('#payment_process_form input[name=receipt_number').val());
                    }else{
                        var receipt_number_adv = $('#payment_process_form input[name=receipt_number').val();
                        var count =0;
                        for(var i = 1; i <= invoices.length - 1; i++){
                                if(invoices.length-1 == i){
                                    balance = parseFloat($('#payment_process_form input[name=balance]').val());
                                    lastins = 1;
                                    last_pa = parseFloat($('#payment_process_form input[name=payment_account]').val()) - for_lastins;
                                    if(last_pa > tmp_amount[i]){
                                        last_pa = tmp_amount[i];
                                    }
                                    last_bal = tmp_amount[i] - last_pa;
                                }else{
                                    lastins = 0;
                                    balance = 0;
                                    for_lastins = for_lastins + tmp_amount[i];
                                    last_pa = tmp_amount[i];
                                    last_bal = 0;
                                }
                                $.ajax({
                                    url:"<?php echo base_url($this->session->userdata('forajax') . '/process_payment'); ?>",
                                    method:'post',
                                    data:$('#payment_process_form').serialize()+'&'+$.param({ 'invoice_id': invoices[i],'invoice_total': total_inv[i],'unpaid_amt': tmp_amount[i], 'pay_amount': last_pa,'balance': last_bal , 'last_ins': lastins, 'account_id':  sessionStorage.account_id, 'receipt_id':resibo_id, 'receipt_app_id': $('#selected_amount').val()}),
                                    dataType:'json',
                                    success: function (data) {
                                        if(data.err == 1){
                                            notifymsg('danger',data.msg);
                                        }else{
                                            receipt_app_id = data.msg;
                                            $('#search_payment').trigger('click');
                                            getReceipt2($('#payment_process_form input[name=receipt_number').val(), $('#selected_amount').val());
                                            getrecnum();
                                            notifymsg('success','<b>Success</b>: Data Saved.');
                                            $('#count_add').val('0');
                                            $('#tab-list-2 #close-add-body').parents('li').remove('li');
                                            $('#add-link').remove();
                                            $('#tab-list-2 a:first').tab('show');
                                        }
                                    },
                                    beforeSend: function(){
                                         $('#process_payment').attr('disabled',true);
                                         $('#loading-icon-process').show();
                                    },
                                    complete: function(){
                                         //$('#receipt').modal('show');
                                         $('#process_payment').removeAttr('disabled');
                                         $('#loading-icon-process').hide();
                                        if(count == 0){
                                            gen_advance_row(receipt_number_adv); count++;
                                        }

                                        
                                    }
                                });
                            }

                             
                    }

                }
            }
            }
            
        });
        
        $('#search_payment_form').keydown(function (e) {
          if (e.which == 13) {
            $('#search_payment_form #search_payment').trigger('click');
            return false;  
          }
        });

        $('#search_payment').click(function(){
                if($('#search_payment_form').valid() ==  true){
                    getrecnum();
                invoices = []; tmp = 0; sum = 0;
                $('#payment_process_form')[0].reset();
                $('#payment_process_form input[name=receipt_date]').val(new Date().toJSON().slice(0,10));
                $("#payment_process_form #process_payment").removeAttr('disabled');
                $.ajax({
                    url:"<?php echo base_url($this->session->userdata('forajax') . '/search_payment'); ?>",
                    method:'post',
                    data:$('#search_payment_form').serialize(),
                    dataType:'json',
                    success: function (data) {
                      $('#tbl_payments').html('');
                      console.log(data.length);
                      var tmp = '';
                      var count = 1;
                      var disable = '';
                      var paid_amount = 0;
                      var sum_pay = 0;
                      var upay = 0;
                      if(data !== 'No results found.' ){
                        notifymsg('success','Record Found');
                        localStorage.acct_name = data[0].acct_name;
                        sessionStorage.account_id = data[0].customer_id;
                        $('#nameofacct').html(data[0].acct_name);
                        $.each(data,function(i,item){
                            paid_amount = 0;
                            disable = '';
                            if(count != 1){
                                disable = 'disabled';
                            }
                            console.log(data[i].sum_pay );
                            console.log(sum_pay);
                            if(data[i].sum_pay == null){
                                sum_pay = parseFloat(0).toFixed(2);
                            }else{
                                sum_pay = parseFloat(data[i].sum_pay).toFixed(2);
                            }
                            paid_amount = parseFloat(data[i].invoice_total).toFixed(2) - sum_pay;

                            if(paid_amount > 0){
                                tmp += "<tr><td class='invoice_id' style='display: none;'>"+data[i].invoice_id+"</td><td align='right'>"+ data[i].invoice_num +"</td><td>"+ data[i].rec_name +"</td><td>"+ data[i].invoice_date +"</td><td>"+ data[i].due_date +"</td><td align='right' class='total_invoice'>"+ parseFloat(data[i].invoice_total).toFixed(2) +"</td><td align='right'>"+ sum_pay +"</td><td class='amount' align='right'>"+ paid_amount.toFixed(2) +"</td><td align='center'><input "+disable+" type='checkbox' id='"+count+"' name='"+data[i].invoice_id+"'></td></tr>"
                                count++;
                                upay += paid_amount;
                            }
                          });
                        $('#unpaid_amount').val(parseFloat(upay).toFixed(2));
                        $('#unpaid_amount1').val(parseFloat(upay).toFixed(2));
                        $('#process_div').show();
                      }else{
                                 notifymsg('warning',"Account doesn\'t exist");
                                 $('#process_div').hide();
                                 tmp = "<tr align='center'><td colspan='8'>No Balance.</td></tr>";
                                 $("#payment_process_form #process_payment").attr('disabled',true);
                            }
                      $('#tbl_payments').html(tmp);
                    },
                    beforeSend: function(){
                        $('#search_payment').attr('disabled',true);
                    },
                    complete: function(){
                        $('#search_payment').removeAttr('disabled');
                    }
                });
                }
        });

        function getrecnum(){
            $.ajax({
                url:"<?php echo base_url($this->session->userdata('forajax') . '/getrecnum'); ?>",
                method:'get',
                dataType:'json',
                success: function (data) {
                    $('#payment_process_form input[name=receipt_number]').val(data[0].val);
                }
            });
        }

        function getReceipt2(id){
            $('#receipt').modal('show');
            $.ajax({
                url: "<?php echo base_url($this->session->userdata('forajax') . '/getReceipt'); ?>",
                method: 'post',
                async: false,
                data: { 'receipt_number' : id },
                dataType:'json',
                success: function (data) {
                    if(data !== 'No results found.'){
                        $('#receipt_payment_form #receipt_number').val(data[0].receipt_number);
                        $('#receipt_payment_form #acct_no').val(data[0].acct_num);
                        $('#receipt_payment_form #payment_type').val(data[0].payment_type);
                        $('#receipt_payment_form #receipt_date').val(data[0].receipt_date);
                        $('#receipt_payment_form #reference').val(data[0].reference);
                        $('#receipt_payment_form #total_selectedss').val(data[0].selected_amt);
                         $('#receipt_payment_form #main_receipt_amt').val(data[0].pay_amount);
                        var tmp = '';
                        var sum_pay = 0;
                        $.each(data,function(i,item) {
                            tmp += '<tr><td align="right">'+ data[i].invoice_num +'</td><td align="right">'+ data[i].invoice_type +'</td><td>'+ data[i].invoice_date +'</td><td align="right">'+ data[i].due_date +'</td><td align="right">'+ data[i].unpaid_amt +'</td><td align="right">'+ data[i].pay_amount +'</td>/tr>';
                        });
                    }else{
                        tmp += '<tr align="center"><td colspan="7">No Result(s)</td></tr>'
                    }
                    $('#receipt_tbl').html(tmp);
                },
                beforeSend: function(){
                    $('#receipt_modal_load').show();
                    $('#receipt .modal-body').hide();
                },
                complete: function(){
                    $('#receipt .modal-body').show();
                    $('#receipt_modal_load').hide();
                },
                error: function(response, status, xhr) {
                  if (status == "error") {
                        var msg = "Sorry but there was an error: ";
                        $('#receipt .modal-body').html(msg + xhr.status + " " + xhr.statusText);
                        $('#receipt_modal_load').hide();
                    }
                }
            });
          //  gen_advance_row(id);
        }

        function getReceipt3(id){
            $('#receipt').modal('show');
            $.ajax({
                url: "<?php echo base_url($this->session->userdata('forajax') . '/getReceipt2'); ?>",
                method: 'post',
                async: false,
                data: { 'receipt_number' : id },
                dataType:'json',
                success: function (data) {
                        $('#receipt_payment_form #receipt_number').val(data[0].receipt_number);
                        $('#receipt_payment_form #acct_no').val(data[0].acct_num);
                        $('#receipt_payment_form #payment_type').val(data[0].payment_type);
                        $('#receipt_payment_form #receipt_date').val(data[0].receipt_date);
                        $('#receipt_payment_form #reference').val(data[0].reference);
                        $('#receipt_payment_form #total_selectedss').val(data[0].payment_amt);
                         $('#receipt_payment_form #main_receipt_amt').val(data[0].pay_amount);
                        $('#receipt_tbl').html('');
                },
                beforeSend: function(){
                    $('#receipt_modal_load').show();
                    $('#receipt .modal-body').hide();
                },
                complete: function(){
                    $('#receipt .modal-body').show();
                    $('#receipt_modal_load').hide();
                },
                error: function(response, status, xhr) {
                  if (status == "error") {
                        var msg = "Sorry but there was an error: ";
                        $('#receipt .modal-body').html(msg + xhr.status + " " + xhr.statusText);
                        $('#receipt_modal_load').hide();
                    }
                }
            });
            //gen_advance_row(id);
        }

        function gen_advance_row(id){
            var unapp =0; var receiptAmt = 0;
         $.ajax({
                url: "<?php echo base_url($this->session->userdata('forajax') . '/genAdvance'); ?>",
                method: 'post',
                data: { 'receipt_number' : id },
                dataType:'json',
                success: function (data) {
                    if(data !== 'No results found.'){
                        var tmp = '';
                        var sum_pay = 0;
                        $.each(data,function(i,item) {
                            tmp += '<tr><td align="right">Advance Payment</td><td align="right"></td><td></td><td align="right"></td><td align="right"></td><td align="right">'+ data[i].applied_amt +'</td></tr>'; unapp = parseFloat(unapp) + parseFloat(data[i].applied_amt);
                            receiptAmt = data[i].payment_amt;
                        });
                    }else{
                        tmp += '<tr align="center"><td colspan="7">No Result(s)</td></tr>'
                    }
                    $('#receipt_tbl').append(tmp);
                    $('#unapp_pay').val(unapp);
                    $('#main_receipt_amt').val(receiptAmt);
                },
                beforeSend: function(){
                    $('#receipt_modal_load').show();
                    $('#receipt .modal-body').hide();
                },
                complete: function(){
                    $('#receipt .modal-body').show();
                    $('#receipt_modal_load').hide();
                },
                error: function(response, status, xhr) {
                  if (status == "error") {
                        var msg = "Sorry but there was an error: ";
                        $('#receipt .modal-body').html(msg + xhr.status + " " + xhr.statusText);
                        $('#receipt_modal_load').hide();
                    }
                }
            });   
        }

        $("#search_payment_form").validate({
             rules: {
                 acct_num: {
                    required: true
                 }
             }
        });

        $('#payment_process_form').validate({
            rules: {
                payment_account: {
                    required: true,
                    number: true
                },
                payment_type: {
                    required: true
                },
                receipt_date: {
                    required: true
                },
            }
        });
        $('#payment_process_form').data('serialize',$('#payment_process_form').serialize());
    });
    </script>